<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $table = 'goods';
    protected $fillable = [
        'name',
        'image',
        'codeno',
        'price',
        'cost_price',
        'status',
        'listorder',
        'content',
        'danwei'
    ];
    public function scopeAllOrSearch($query,$searchQuery = null)
    {
        if (is_null($searchQuery)) {
            return $query->paginate(config('admin.goods.perpage'));
        }else{
            $search="%{$searchQuery}%";
            return $query->where('name','like',$search)
                    ->orWhere('codeno', 'like', $search)
                    ->paginate(config('admin.goods.perpage'));
        }
    }
    public function scopeEnable($query){
        return $query->where('status',1);
    }
    public function scopeSearch($query,$searchQuery = null){
        if (!is_null($searchQuery)) {
            $search="%{$searchQuery}%";
            return $query->where('name','like',$search)
                    ->orWhere('codeno', 'like', $search)
                    ->get();
        }
    }
}
